﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 向下选取命令。
    /// ★①，修改类名。
    /// </summary>
    public static class MindViewSelectDownCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static MindViewSelectDownCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "MindViewSelectDownCommand",
                "MindViewSelectDownCommand",
                typeof(MindViewSelectDownCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.Down,ModifierKeys.None,"Down")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            Execute();
        }

        internal static void Execute()
        {
            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                Globals.MainWindow.SelectedTitleImage.MasterTitle.SelectOnlySelf();
                return;
            }

            if (Globals.SelectedTitlesList.Count <= 0)
            {
                var selComment = Globals.MainWindow.SelectedComment;
                if (selComment != null && selComment.MasterTitle != null)
                {
                    selComment.MasterTitle.SelectOnlySelf(); return;
                }

                var selPictureBox = Globals.MainWindow.SelectedPictureBox;
                if (selPictureBox != null && selPictureBox.MasterTitle != null)
                {
                    selPictureBox.MasterTitle.SelectOnlySelf(); return;
                }

                Globals.MainWindow.RootTitle.SelectOnlySelf();
            }
            else if (Globals.SelectedTitlesList.Count > 1)
            {
                Title mainSelectedTitle = null;
                foreach (Title t in Globals.SelectedTitlesList)
                {
                    if (t.IsMainSelectedTitle)
                    {
                        mainSelectedTitle = t;
                    }
                }

                if (mainSelectedTitle == null)
                {
                    Globals.MainWindow.RootTitle.SelectOnlySelf();
                }
                else
                {
                    if (mainSelectedTitle.IsFixed)
                    {
                        Title higher = mainSelectedTitle.GetHigherLvelTitle();
                        if (higher != null)
                        {
                            higher.SelectOnlySelf();
                        }
                        else
                        {
                            Globals.MainWindow.RootTitle.SelectOnlySelf();
                        }
                    }
                    else
                    {
                        mainSelectedTitle.SelectOnlySelf();
                    }
                }
            }
            else
            {
                //分情况：
                Title t = Globals.SelectedTitlesList[0];
                if (t == Globals.MainWindow.RootTitle) return;//根标题直接返回，不作任何其它操作。

                Title higher = t.GetHigherLvelTitle();
                if (higher == null) return;

                if (t.IsFixed)
                {
                    higher.SelectOnlySelf(); return;
                }

                if (t.Index == higher.LowerLevelTitles.Count - 1)
                {
                    higher.SelectOnlySelf();
                    return;
                }


                //如果不是最大索引。
                if (t.HigherLevelTitleID == Globals.MainWindow.RootTitle.ID)
                {
                    //一级.
                    Title preSameLevelTitle = null;

                    if (higher == null || higher.LowerLevelTitles.Count <= 0) return;//这里是出错了。

                    for (int i = t.Index + 1; i < higher.LowerLevelTitles.Count; i++)
                    {
                        Title tBrother = higher.LowerLevelTitles[i];
                        if (tBrother.Index > t.Index && t.Direction == tBrother.Direction)
                        {
                            preSameLevelTitle = tBrother;
                            break;
                        }
                    }

                    if (preSameLevelTitle != null) { preSameLevelTitle.SelectOnlySelf(); return; } else return;
                }
                else
                {
                    Title nextSameLevelTitle = null;

                    if (higher == null || higher.LowerLevelTitles.Count <= 0) return;//这里是出错了。

                    for (int i = t.Index + 1; i < higher.LowerLevelTitles.Count; i++)
                    {
                        Title tBrother = higher.LowerLevelTitles[i];
                        if (tBrother.Index > t.Index)
                        {
                            nextSameLevelTitle = tBrother;
                            break;
                        }
                    }

                    if (nextSameLevelTitle != null) { nextSameLevelTitle.SelectOnlySelf(); return; } else return;
                }
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = true;//总是可用。
        }
    }
}
